package com.leoman.recordcount.service.impl;

import com.leoman.common.service.impl.GenericManagerImpl;
import com.leoman.recordcount.dao.RecordCountDao;
import com.leoman.recordcount.entity.RecordCount;
import com.leoman.recordcount.service.RecordCountService;
import com.leoman.systemsetting.dao.UserTaskDao;
import com.leoman.systemsetting.entity.UserTask;
import com.leoman.user.dao.UserInvitedDao;
import com.leoman.user.entity.UserInvited;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2016/7/22 0022.
 */
@Service
public class RecordCountServiceImpl extends GenericManagerImpl<RecordCount, RecordCountDao> implements RecordCountService {

    @Autowired
    private RecordCountDao recordCountDao;

    @Autowired
    private UserInvitedDao userInvitedDao;

    @Autowired
    private UserTaskDao userTaskDao;

    @Override
    public void addCount(Integer visitCount, Integer onlineCount) {
        RecordCount recordCount = recordCountDao.findOne(1l);
        recordCount.setVisitCount(recordCount.getVisitCount() + visitCount);
        recordCount.setOnlineCount(recordCount.getOnlineCount() + onlineCount);

        recordCountDao.save(recordCount);
    }

    @Override
    public void clearCount() {
        RecordCount recordCount = recordCountDao.findOne(1l);
        recordCount.setVisitCount(0);
        recordCount.setOnlineCount(0);

        recordCountDao.save(recordCount);
    }

    @Override
    @Transactional
    public void clearInvitedCount() {
        List<UserInvited> list = userInvitedDao.findValidList();

        for (UserInvited userInvited : list) {
            userInvited.setCount(0);
            userInvited.setCoin(0);
            userInvited.setIsShare(0);

            userInvitedDao.save(userInvited);
        }
    }

    @Override
    @Transactional
    public void clearTask() {
        List<UserTask> list = userTaskDao.findValidList();

        for (UserTask userTask : list) {
            userTask.setCount(0);
            userTask.setIsGet(0);

            userTaskDao.save(userTask);
        }
    }
}
